﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit.DramaDialog
{
    public partial class EditDramaSetExitUnitDialog : Form
    {
        PListDict curDict;
        public EditDramaSetExitUnitDialog()
        {
            InitializeComponent();
            foreach (Hero hero in UCDrama.heros)
            {
                comboBoxHero.Items.Add(hero.name);
            }
            foreach (KeyValuePair<int, string> kv in Constant.ownTypeDic)
            {
                comboBoxOwner.Items.Add(kv.Value);
            }
        }
        public void Show(IWin32Window owner, PListDict dict)
        {
            curDict = dict;
            PListInteger dataType = (PListInteger)curDict["DataType"];
            PListDict data = (PListDict)curDict["Data"];
            PListInteger bfIndex = (PListInteger)data["BFIndex"];
            PListInteger unitID = (PListInteger)data["UnitID"];
            PListInteger originX = (PListInteger)data["OriginX"];
            PListInteger originY = (PListInteger)data["OriginY"];
            PListInteger dstX = (PListInteger)data["DstX"];
            PListInteger dstY = (PListInteger)data["DstY"];
            PListInteger ownerType = (PListInteger)curDict["Owner"];
            PListInteger instructType = (PListInteger)curDict["InstructType"];
            if (instructType.Value == (long)kInstructType.kInstructTypeBattleExit)
            {
                PListBool isDead = (PListBool)curDict["IsDead"];
                checkBox1.Checked = isDead.Value;
                checkBox1.Visible = true;
            }
            else
                checkBox1.Visible = false;

            comboBoxDataType.SelectedIndex = (int)dataType.Value;
            textBoxBFIndex.Text = bfIndex.Value.ToString();
            textBoxDstX.Text = dstX.Value.ToString();
            textBoxDstY.Text = dstY.Value.ToString();
            textBoxOriginX.Text = originX.Value.ToString();
            textBoxOriginY.Text = originY.Value.ToString();
            comboBoxOwner.SelectedIndex = (int)ownerType.Value;
            
            Hero hero = UCDrama.heros.Find((h) => { return h.num == unitID.Value; });
            comboBoxHero.SelectedItem = hero.name;
            ShowDialog(owner);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            curDict["DataType"] = new PListInteger(comboBoxDataType.SelectedIndex);
            PListDict data = (PListDict)curDict["Data"];
            if (comboBoxHero.Text != null)
            {
                Hero hero = UCDrama.heros.Find((h) => { return h.name == comboBoxHero.Text.ToString(); });
                if (hero != null)
                    data["UnitID"] = new PListInteger(hero.num);
                else
                    data["UnitID"] = new PListInteger(0);
            }
            data["BFIndex"] = new PListInteger(System.Int32.Parse(textBoxBFIndex.Text));
            data["OriginX"] = new PListInteger(System.Int32.Parse(textBoxOriginX.Text));
            data["OriginY"] = new PListInteger(System.Int32.Parse(textBoxOriginY.Text));
            data["DstX"] = new PListInteger(System.Int32.Parse(textBoxDstX.Text));
            data["DstY"] = new PListInteger(System.Int32.Parse(textBoxDstY.Text));
            curDict["Owner"] = new PListInteger(comboBoxOwner.SelectedIndex);
            if (checkBox1.Visible)
                curDict["IsDead"] = new PListBool(checkBox1.Checked);
            this.Hide();
        }

        private void comboBoxDataType_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxBFIndex.Enabled = false;
            groupBox1.Enabled = false;
            comboBoxHero.Enabled = false;
            if (comboBoxDataType.SelectedIndex == 1)
            {
                comboBoxHero.Enabled = true;
            }
            else if (comboBoxDataType.SelectedIndex == 0)
            {
                textBoxBFIndex.Enabled = true;
            }
            else
                groupBox1.Enabled = true;
        }
    }
}
